package com.chinasoft.mall.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
//	System.out.println("year:"+fieldDifference(d.getTime(), System.currentTimeMillis(), Calendar.YEAR));
//	System.out.println("month:"+fieldDifference(d.getTime(), System.currentTimeMillis(), Calendar.MONTH));
//	System.out.println("date:"+fieldDifference(d.getTime(), System.currentTimeMillis(), Calendar.DATE));
//	System.out.println("hour:"+fieldDifference(d.getTime(), System.currentTimeMillis(), Calendar.HOUR));
//	System.out.println("minute:"+fieldDifference(d.getTime(), System.currentTimeMillis(), Calendar.MINUTE));
//	System.out.println("second:"+fieldDifference(d.getTime(), System.currentTimeMillis(), Calendar.SECOND));
	public static int fieldDifferenceNow(/*String strtime1, */String strtime2, int field) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long time1=0;
		long time2=0;
		try {
			time1 = /*sdf.parse(strtime1)*/new Date().getTime();
			time2 =sdf.parse(strtime2).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (time1 == time2) {
			return 0;
		} else if (time1 > time2) {
			// 确保time1比time2小
//			return fieldDifference(strtime2, strtime1, field);
			try {
				throw new Exception("时间比较异常");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// 下面清除不要参与比较的内容
		Calendar cal1 = Calendar.getInstance();
		cal1.setLenient(false);
		cal1.setTimeInMillis(time1);
		Calendar cal2 = Calendar.getInstance();
		cal2.setLenient(false);
		cal2.setTimeInMillis(time2);
		for (int x = 0; x < Calendar.FIELD_COUNT; x++) {
			if (x > field) {
				cal1.clear(x);
				cal2.clear(x);
			}
		}
		// 重新设定初始值
		time1 = cal1.getTimeInMillis();
		time2 = cal2.getTimeInMillis();
		long ms = 0;
		int min = 0;// 下限,从0开始
		int max = 1;// 每次所加的值,第一次加1
		while (true) {
			// 因为max的值都是相对time1而言,故每次都是从time1开始而不是ms
			cal1.setTimeInMillis(time1);
			cal1.add(field, max);// 将field对应的字段加上max的值
			ms = cal1.getTimeInMillis();
			if (ms == time2) {
				// 两个时间之间相差的值为max
				min = max;
				break;
			} else if (ms > time2) {
				// 超过后,则差值肯定小于max
				break;
			} else {
				// 仍然小于time2,继续增大max,直到ms>=time2为止
				max <<= 1;
			}
		}
		// 上面的操作中没有找到准确的值,接下来使用二分查找以准确找出差值
		while (max > min) {
			cal1.setTimeInMillis(time1);
			int t = (min + max) >>> 1;
			cal1.add(field, t);
			ms = cal1.getTimeInMillis();
			if (ms == time2) {
				min = t;
				break;
			} else if (ms > time2) {
				max = t;
			} else {
				min = t;
			}
		}
		return min;
	}

	/**
	 * 昨天日期
	 * 
	 * @return 2012-10-17 14:59:03
	 */
	public static String yesterdayDate() {

		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		String yesterday = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(cal.getTime());
		return yesterday;
	}

	/**
	 * 今天日期
	 * 
	 * @return 2012-10-18 14:59:03
	 */
	public static String nowDate() {

		Calendar cal = Calendar.getInstance();
		String yesterday = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(cal.getTime());
		return yesterday;
	}

	/**
	 * 明天日期
	 * 
	 * @return 2012-10-19 14:59:03
	 */
	public static String tomorrowDate() {

		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 1);
		String yesterday = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(cal.getTime());
		return yesterday;
	}

	/**
	 * 
	 * @param beginTime
	 *            起始时间
	 * @param endTime
	 *            结束时间
	 * @return -1 等待直播 0 正在直播 1 直播完毕 -2 错误
	 * @throws ParseException
	 */
	public static byte CompareTime(String beginTime, String endTime) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date ndate = new Date();
			Date begin = df.parse(beginTime);
			Date end = df.parse(endTime);
			if ((begin.getTime() < ndate.getTime())
					&& (end.getTime() > ndate.getTime())) {
				return 0;
			} else if (end.getTime() < ndate.getTime()) {
				return 1;
			} else if (begin.getTime() > ndate.getTime()) {
				return -1;
			} else {
				return -2;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return -2;
		}
	}

	/**
	 * 比较两个时间
	 * 
	 * @param one
	 * @param another
	 * @return 1 one 时间比较大 -1 another 时间比较大
	 */
	public static byte CompareTime1(String one, String another) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date oneDate = df.parse(one);
			Date anotherDate = df.parse(another);
			if (oneDate.getTime() > anotherDate.getTime()) {
				return 1;
			} else if (oneDate.getTime() < anotherDate.getTime()) {
				return -1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 时间格式转换
	 * 
	 * @param time
	 *            2012-05-18 15:14:03
	 * @return 5月18日
	 */
	public static String formatDate(String time) {
		String formatStr;
		try {
			SimpleDateFormat fomat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = fomat.parse(time);
			fomat = new SimpleDateFormat("MM月dd日");
			formatStr = fomat.format(date);
			if (formatStr.startsWith("0")) {
				formatStr = formatStr.substring(1);
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		return formatStr;
	}

	/**
	 * 时间格式转换
	 * 
	 * @param time
	 *            2012-05-18 15:14:03
	 * @return 3:14PM
	 */
	public static String format1Date(String time) {
		String formatStr;
		Calendar calendar = new GregorianCalendar();
		try {
			SimpleDateFormat fomat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = fomat.parse(time);
			calendar.setTime(date);
			fomat = new SimpleDateFormat("hh:mm");
			formatStr = fomat.format(date);
			if (formatStr.startsWith("0")) {
				formatStr = formatStr.substring(1);
			}
			String ampmValues;
			if (calendar.get(Calendar.AM_PM) == 0) {
				ampmValues = "AM";
			} else {
				ampmValues = "PM";
			}
			formatStr += ampmValues;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		return formatStr;
	}

	public static String format2Date(String beginTime, String endTime) {
		String formatStr = null;
		String toStr = null;
		SimpleDateFormat fomat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date begin = null;
		Date end = null;
		try {
			begin = fomat.parse(beginTime);
			end = fomat.parse(endTime);
			fomat = new SimpleDateFormat("HH:mm");
			formatStr = fomat.format(begin);
			toStr = fomat.format(end);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return formatStr + "-" + toStr;
	}

	public static boolean compare3Date(String time) {
		String formatStr;
		String nowStr;
		try {
			SimpleDateFormat fomat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = fomat.parse(time);
			Date now = new Date();
			fomat = new SimpleDateFormat("HH:mm");

			formatStr = fomat.format(date);
			nowStr = fomat.format(now);
			if (formatStr.equals(nowStr)) {
				return true;
			} else {
				return false;
			}

		} catch (ParseException e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 获取当前日期是周几<br>
	 * 
	 * @param dt
	 * @return 当前日期是周几
	 */
	public static String getWeekOfDate(String time) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dt = null;
		try {
			dt = df.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);

		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;

		return weekDays[w];
	}

}
